package com.hotkid.ceo.jetpack.myjetpackmvvm.ui.splash

import android.content.Intent
import android.os.CountDownTimer
import com.gyf.immersionbar.ImmersionBar
import com.hotkid.ceo.jetpack.myjetpackmvvm.R
import com.hotkid.ceo.jetpack.myjetpackmvvm.base.BaseActivity
import com.hotkid.ceo.jetpack.myjetpackmvvm.common.MyConfig
import com.hotkid.ceo.jetpack.myjetpackmvvm.databinding.ActivitySplashBinding
import com.hotkid.ceo.jetpack.myjetpackmvvm.ui.main.MainActivity
import com.hotkid.ceo.jetpack.myjetpackmvvm.ui.login.LoginActivity
import com.hotkid.ceo.jetpack.myjetpackmvvm.util.setOnclickNoRepeat
import com.yechaoa.yutilskt.SpUtil

/**
 * ClassName:SplashActivity
 * Package:com.hotkid.ceo.jetpack.myjetpackmvvm.ui.splash
 * Description:
 * @date:2021/3/25 11:11 AM
 * @author:774169396@qq.com
 */
class SplashActivity : BaseActivity<ActivitySplashBinding>() {
    private lateinit var timer: CountDownTimer
    override fun getViewBinding(): ActivitySplashBinding {
        return ActivitySplashBinding.inflate(layoutInflater)
    }

    override fun initialize() {
        super.initialize()
        ImmersionBar
                .with(this)
                .fitsSystemWindows(true)
                .barColor(R.color.transparent)
                .init()
        setListener()
    }


    override fun onResume() {
        super.onResume()
        startCountDown()
    }

    private fun startCountDown() {
        timer=object :CountDownTimer(1*1000,1000){
            override fun onTick(millisUntilFinished: Long) {
                timer.cancel()
                checkLogin()
            }

            override fun onFinish() {
                timer.cancel()
                checkLogin()
            }


        }

        timer.start()
    }

    private fun setListener() {
        mBinding.tvSkip.setOnclickNoRepeat {
            if (this::timer.isInitialized) timer.cancel()
            checkLogin()
        }
    }

    private fun checkLogin() {
        if (!SpUtil.getBoolean(MyConfig.IS_LOGIN)) {
            startActivity(Intent(this, LoginActivity::class.java))
            finish()
        } else {
            startActivity(Intent(this, MainActivity::class.java))
        }
    }


}